package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.utill.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: java类作用描述
 * @Author: wuyuanhao
 * @CreateDate: 2020/8/11 18:13
 * @UpdateUser: wuyuanhao
 * @UpdateDate: 2020/8/11 18:13
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;

    //跳用户管理页面
    @GetMapping("/userManage")
    public String user(HttpSession session, Model model){
        User user= (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "user/userManage";
    }

    //用户
    @GetMapping("/list")
    @ResponseBody
    @CrossOrigin//解决跨域问题
    public List<User> list(User user){
        List<User> list=userService.selectUserList(user);
        return list;
    }

    //通过用户id查询用户数据
    @GetMapping("/seUserById")
    @ResponseBody
    @CrossOrigin//解决跨域问题
    public User seUserById(int id){

        return userService.seUserById(id);
    }

    //修改用户
    @PostMapping("/updateUser")
    @ResponseBody
    @CrossOrigin//解决跨域问题
    public Map<String,Object> updateUser(@RequestBody User user){
        Map<String,Object> map=new HashMap<String,Object>();
        if(userService.updateUser(user)>0){
            map.put("success",true);
            return map;
        }
        map.put("success",false);
        return map;
    }

    //添加用户
    @PostMapping("/addUser")
    @ResponseBody
    public Map<String,Object> addUser(@RequestBody User user){
        Map<String,Object> map=new HashMap<String,Object>();
        try {
            userService.addUser(user);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

    //登录查询
    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> seUser(User user, HttpSession session){
        User user1=userService.login(user);
        Map<String,Object> map=new HashMap<String,Object>();
        if (user1!=null){
//            model.addAttribute("user",user1);
            map.put("success",true);
            session.setAttribute("user",user1);
            return map;
        }
        map.put("success",false);
        return map;
    }

    //通过用户id删除用户数据
    @GetMapping("/deleteById")
    @ResponseBody
    @CrossOrigin//解决跨域问题
    public Map<String,Object> deleteById(int id){
        Map<String,Object> map=new HashMap<String,Object>();
        if(userService.deleteById(id)>0){
            map.put("success",true);
            return map;
        }
        map.put("success",false);
        return map;
    }
}
